package priv.dajie.tzzq.spider;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import priv.dajie.tzzq.proxy.Proxy;
import priv.dajie.tzzq.proxy.ProxyPoolLocal;
import priv.dajie.tzzq.spider.dota.buff.BuffDotaSpider;
import priv.dajie.tzzq.spider.dota.buff.BuffInfos;
import priv.dajie.tzzq.spider.dota.c5.C5DotaSpider;
import priv.dajie.tzzq.spider.dota.c5.C5Infos;
import priv.dajie.tzzq.spider.dota.steam.SteamDotaSpider;
import priv.dajie.tzzq.spider.model.SpiderInfo;
import priv.dajie.tzzq.spider.model.SpiderMode;
import priv.dajie.tzzq.spider.dota.steam.SteamInfos;
import priv.dajie.tzzq.common.Tools;

public class SpiderManager {


    private int c5ThreadNum;
    private int buffThreadNum;
    private int steamThreadNum;
    private Logger logger = LogManager.getLogger(this.getClass());
    private static SpiderManager instance = new SpiderManager();


    public static SpiderManager getInstance() {
        return instance;
    }

    private SpiderManager() {
        c5ThreadNum = 10;
        buffThreadNum = 2;
        steamThreadNum = 1;
    }

    public void startC5Spider() {
        SpiderInfo spiderInfo = C5Infos.getC5Info();
        Thread[] spiderThread = new Thread[c5ThreadNum];
        // 创建线程并启动
        for (int i = 0; i < c5ThreadNum; i++) {
            logger.info("启动c5爬虫:线程" + i);
            C5DotaSpider c5Spider = new C5DotaSpider(SpiderMode.NEWTABLE);
            c5Spider.setSpiderInfo(spiderInfo);
            spiderThread[i] = new Thread(c5Spider);
            spiderThread[i].start();
        }

        // 把线程加入到主线程
        for (int i = 0; i < c5ThreadNum; i++) {
            try {
                spiderThread[i].join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }

    public void startBuffSpider() {
        SpiderInfo spiderInfo = BuffInfos.getBuffInfo();
        Proxy[] proxies = ProxyPoolLocal.getInstance().getLocalProxys();
        Thread[] spiderThread = new Thread[buffThreadNum+proxies.length];
        // 创建线程并启动
        for (int i = 0, s=0; i < buffThreadNum+proxies.length; i++) {
            BuffDotaSpider buffSpider;
            if (i < buffThreadNum){
                buffSpider = new BuffDotaSpider(SpiderMode.NEWTABLE);
                logger.info("启动buff爬虫:线程" + i);
            }
            else{
                buffSpider = new BuffDotaSpider(SpiderMode.NEWTABLE, proxies[s++]);
                logger.info("启动buff爬虫(代理):线程" + s);
            }
            buffSpider.setSpiderInfo(spiderInfo);
            spiderThread[i] = new Thread(buffSpider);
            spiderThread[i].start();
        }

        // 把线程加入到主线程
        for (int i = 0; i < buffThreadNum+proxies.length; i++) {
            try {
                spiderThread[i].join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public void startSteamSpider() {
        SpiderInfo spiderInfo = SteamInfos.getSteamInfos();
        Thread[] spiderThread = new Thread[steamThreadNum];
        for (int i = 0; i < steamThreadNum; i++) { // 创建线程并启动
            logger.info("启动steam爬虫:线程" + i);
            SteamDotaSpider steamSpider = new SteamDotaSpider();
            steamSpider.setSpiderInfo(spiderInfo);
            spiderThread[i] = new Thread(steamSpider);
            spiderThread[i].start();
        }
        for (int i = 0; i < steamThreadNum; i++) { // 把线程加入到主线程
            try {
                spiderThread[i].join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public void startAllSpider() {
        startSteamSpider();
        Tools.saveLastRunTime(System.currentTimeMillis());
        startC5BuffSpdier();
    }

    public void startC5BuffSpdier() {
        startC5Spider();
        startBuffSpider();
    }

    public int getC5ThreadNum() {
        return c5ThreadNum;
    }

    public void setC5ThreadNum(int c5ThreadNum) {
        this.c5ThreadNum = c5ThreadNum;
    }

    public int getBuffThreadNum() {
        return buffThreadNum;
    }

    public void setBuffThreadNum(int buffThreadNum) {
        this.buffThreadNum = buffThreadNum;
    }

    public int getSteamThreadNum() {
        return steamThreadNum;
    }

    public void setSteamThreadNum(int steamThreadNum) {
        this.steamThreadNum = steamThreadNum;
    }

    public static void main(String[] args) {
        SpiderManager.getInstance().startC5BuffSpdier();
    }
}
